package cn.sdormitory.smartdor.dao;

import cn.sdormitory.basedata.entity.BDevice;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.vo.AttenceVo;
import cn.sdormitory.basedata.vo.BStudentListVo;
import cn.sdormitory.smartdor.entity.OriginalRecord;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.entity.SdStudentAttenceDetail;
import cn.sdormitory.smartdor.entity.StudentTrajectory;
import cn.sdormitory.smartdor.vo.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  Created By ruanteng
 *  DateTime：2020/11/27
 */
@Mapper
public interface SdAttenceDao extends BaseMapper<SdAttence> {

    /**
     * 获取指定日期的考勤信息
     * @param date
     * @param currIndex
     * @param pageSize
     * @return
     */
    List<SdAttence> getList(@Param("date")String date,@Param("currIndex")Integer currIndex,@Param("pageSize") Integer pageSize);

    /**
     * 获取总记录条数
     * @param date
     * @return
     */
    int getListCount(@Param("date")String date);

    /**
     * 获得缺勤学生信息
     * @return
     */
    List<SdAttenceVo> listAbsenceStudent(Map<String,Object> map);

    /**
     * 获得请假学生信息
     * @return
     */
    List<SdAttenceVo> listLeaveStudent(Map<String,Object> map);

    /**
     * 获得每个宿舍的考勤信息
     * @param map
     * @return
     */
    List<DormitoryAttenceVo> dormitoryAttenceVos(Map<String,Object> map);

    /**
     * 获得当天缺勤的学生
     * @return
     */
    List<SdAttenceVo> getLackStu();

    //查询每天晚归的记录
    SdAttence getCombacklate(@Param("studentNo")String studenNo, @Param("accessDate")String accessDate);

    //APP首页查询宿舍考勤列表
    List<DormitoryAttenceVo> getAppDorAttence(@Param("checkDate")Date checkDate);

    //APP端查询缺勤学员列表
    List<SdAttenceVo> getAppAbsenceStudent(@Param("checkDate")Date checkDate);

    int selectAttenceByStudentCount(@Param("studentNo") String studentNo);

    /**
     * 根据考勤时间和宿舍ID 查询考勤信息
     * @param accessDate
     * @param bdormitoryId
     * @return
     */
    List<SdAttence> getAttenceByDateAndBdormitoryId(@Param("accessDate") String accessDate,@Param("bdormitoryId") Long bdormitoryId) ;

    /**
     * 根据考勤时间和学生编号查询考勤信息
     * @param accessDate
     * @param studentNo
     * @return
     */
    List<SdAttence> getAttenceByDateAndStudentNo(@Param("accessDate") String accessDate,@Param("studentNo") String studentNo) ;

    /**
     * 根据学号修改考勤状态
     * @param studentNo
     * @param attenceStatus
     * @return
     */
    int updateAttenceStatus(@Param("studentNo") String studentNo,@Param("accessDate") String accessDate,@Param("attenceStatus") String attenceStatus);

    List<DormitoryAttenceVo> getAppDorAttenceManual(@Param("className")String className, @Param("storey")String storey,@Param("accessDate")String accessDate,@Param("buildingNo")String buildingNo);

    int deleteByStuNoAndADate(@Param("studentNo") String studentNo,@Param("accessDate") String accessDate);

    public AttenceVo getAttenceBySdormitoryid(@Param("bdormitoryId") Long bdormitoryId,@Param("accessDate") String accessDate);

    int getAttByDateAndStuNoCount(@Param("studentNo") String studentNo,@Param("accessDate") String accessDate);

//    @Select("SELECT * FROM sd_attence WHERE student_no = #{studentNo} AND attence_status != '1' ")
    List<SdAttenceAppVo> findSdAttenceByStuNo(@Param("studentNo")String studentNo);
    
    IPage<SdAttenceAppVo> getAttencePageByStudentNo(String studentNo, IPage<SdAttenceAppVo> page);
    
    SdAttenceAppVo getStudentDorAttendance(String studentNo, String datetime);

    /**
     * 查询宿舍考勤列表
     * @return
     */
    List<SdAttenceAppVo> findList(@Param("startDate")String startDate,
                             @Param("endDate")String endDate,
                             @Param("buildingNo")String buildingNo,
                             @Param("storey")String storey,
                             @Param("dormitoryNo")String dormitoryNo);

    @Select("SELECT count(*) FROM sd_attence WHERE attence_status = '1' AND student_no = #{studentNo} ")
    Integer findStuAttenceStatus1Count(@Param("studentNo")String studentNo);

    @Select("SELECT count(*) FROM sd_attence WHERE attence_status = '2' AND student_no = #{studentNo} ")
    Integer findStuAttenceStatus2Count(@Param("studentNo")String studentNo);

    @Select("SELECT count(*) FROM sd_attence WHERE attence_status = '3' AND student_no = #{studentNo} ")
    Integer findStuAttenceStatus3Count(@Param("studentNo")String studentNo);

    @Select("SELECT count(*) FROM sd_attence WHERE attence_status = '4' AND  student_no = #{studentNo} ")
    Integer findStuAttenceStatus4Count(@Param("studentNo")String studentNo);

    List<SdAttenceAppVo> findStuUnusualAttenceList(@Param("studentNo")String studentNo);

    /**
     * 查询当天宿舍考勤所有打卡记录
     * @param accessDate
     * @return
     */
    List<SdAttence> getAllTodayRecords(@Param("accessDate") String accessDate);
    
    List<SdStudentAttenceDetail> getAllWeekendRecords(@Param("accessDate") String accessDate);
    
    List<SdStudentAttenceDetail> getAllDormitoryRecords(@Param("accessDate") String accessDate);
    
    /**
     * 查询周末当天打卡情况
     * @author alex
     * @param: accessDate
     * @date 2024/9/14 8:57
     * @return
     */
    List<SdAttence> getWeekendClockInData(@Param("accessDate") String accessDate, @Param("startDate") String startDate, @Param("endDate") String endDate);

    /**
     *  查询指定宿舍指定日期下未归寝人数
     * @param accessDate 考勤日期
     * @param dormitoryId 宿舍Id
     * @return
     */
    int getNotReturnToBed(@Param("accessDate")String accessDate, @Param("dormitoryId")Long dormitoryId);

    /**
     * 批量插入
     * @param sdAttendanceList
     */
    void insertBatch(@Param("sdAttendanceList") List<SdAttence> sdAttendanceList);

    /**
     * 根据宿舍考勤状态获取指定日期下指定用户所管辖的所有班级的学生名单
     * @param userId
     * @param accessDate
     * @param attenceStatus
     * @return
     */
    List<BStudent> getClassUnderJurisdictionOfStuByStatus(@Param("userId")Long userId,
                                                       @Param("accessDate")String accessDate,
                                                       @Param("attenceStatus")String attenceStatus);

    /**
     * 根据日期和部门查询缺勤学生人数
     * @param accessDate
     * @param classDept
     * @return
     */
    Integer getAbsenceStuByDeptAndDept(@Param("accessDate")String accessDate,
                                     @Param("classDept")String classDept);
    
    List<BStudentListVo> getAbsenceStudentsByCheckDateAndDeptId(@Param("accessDate")String accessDate,
                                       @Param("classDept")String classDept,
                                                                @Param("classId") String classId);
//    /**
//     * 根据相关条件获取宿舍考勤数据
//     * @param condition
//     * @return
//     */
//    List<SdAttence> getSdAttenceByCondition(@Param("condition")SdAttence condition);

    List<SdAttence> getSdAttenceExcList(@Param("query")SdAttence query);

    /**
     * 根据考勤日期和楼栋楼层查询未归寝的考勤学生数据
     * @param accessDate
     * @param buildingNo
     * @param storey
     * @param startNum
     * @param pageSize
     * @return
     */
    List<BStudent> getSdAttenceStu(@Param("accessDate")String accessDate,
                                   @Param("buildingNo")String buildingNo,
                                   @Param("storey")String storey,
                                   @Param("startNum")int startNum,
                                   @Param("pageSize")int pageSize);

    Integer getSdAttenceStuCount(@Param("accessDate")String accessDate,
                                 @Param("buildingNo")String buildingNo,
                                 @Param("storey")String storey);

    /**
     * 根据学号和考勤日期获取该学生考勤数据
     * @param accessDate
     * @param studentNo
     * @return
     */
    List<SdAttence> getSdAbsencentByDataAndStuNo(@Param("accessDate")String accessDate,
                                                 @Param("studentNo")String studentNo);

    List<BDevice> getDeviceNo(@Param("buildingNo")String buildingNo, @Param("storey")String storey);

    List<SdAttence> getMendCardStuList(@Param("query")SdAttence query);

    /**
     * 根据日期楼栋楼层查询出勤人数
     * @param buildingNo
     * @param storey
     * @param accessDate
     * @param attenceStatus 考勤状态（传'1'时候查询出勤状态为正常人数，传null为查询当日当层所有考勤人数包含请假，晚归和未归寝）
     * @return
     */
    int getAttenceCountByBuildingStoreyAndDate(@Param("buildingNo")String buildingNo,
                                               @Param("storey")String storey,
                                               @Param("accessDate")String accessDate,
                                               @Param("attenceStatus")String attenceStatus);

    /**
     * 考勤统计
     * @param buildingNo
     * @param storey
     * @param accessDate
     * @param attenceStatus
     * @return
     */
    List<SdAttence> getStuByBuildStorey(@Param("buildingNo")String buildingNo,
                                        @Param("storey")String storey,
                                        @Param("accessDate")String accessDate,
                                        @Param("attenceStatus")String attenceStatus);

    /**
     * 出勤异常统计
     * @param attenceStauts
     * @return
     */
    List<SdAttenceReportVo> getStuExcAttence(@Param("startDate")String startDate,
                                             @Param("endDate")String endDate,
                                             @Param("attenceStatus")String attenceStauts,
                                             @Param("buildingNo")String buildingNo);

    List<String> getAllStudentNo(@Param("accessDate")String accessDate,
                                 @Param("buildingNo")String buildingNo,
                                 @Param("storey")String storey);
    
    
    List<BStudentListVo> getWeekendClockInStudentByCId(@Param("classId") Long classId, @Param("accessDate") String accessDate);
	
    /**
     * 根据班级id和日期查看晚寝未打卡名单
     * @author alex
     * @param: classId
     * @param: accessDate
     * @date 2024/9/30 10:49
     * @return
     */
	List<BStudentListVo> getDormitoryUnclcokListByClassId(Long classId, String accessDate);
    
    /**
     * 根据部门id和日期查看周末未打卡名单
     * @author alex
     * @param: checkDate
     * @param: valueOf
     * @date 2024/10/10 14:51
     * @return
     */
    List<BStudentListVo> getWeekendAbsenceStudentsByCheckDateAndDeptId(@Param("accessDate")String accessDate,
                                                                @Param("classDept")String classDept,
                                                                       @Param("classId") String classId);
    
    // 根据班级id 时间 考勤状态获取学生列表
    List<BStudentListVo> getAttenceStusByClassId(@Param("classId") Long classId,
                                                 @Param("accessDate") String accessDate,
                                                 @Param("attenceStatus") String attenceStatus);
    
    // 周末考勤根据班级id 时间 考勤状态获取学生列表
    List<BStudentListVo> getWeekendAttenceStusByClassId(@Param("classId") Long classId,
                                                 @Param("accessDate") String accessDate,
                                                 @Param("attenceStatus") String attenceStatus);
    
    // 根据时间段和学生id删除宿舍考勤表中的请假数据
    void deleteDormitoryLeaveAttenceInfo(@Param("studentNo") String studentNo,
                                            @Param("startDate") String startDate,
                                            @Param("endDate") String endDate);
    
    // 根据班级id和日期查询出所有考勤数据
    List<SdStudentAttenceDetail> getAllDormitoryRecordsByClassIds(@Param("classIds") List<Long> classIds,
                                                                  @Param("accessDate") String accessDate,
                                                                  @Param("attenceStatus") String attenceStatus);
    
    // 根据班级id和日期查询出所有周末考勤数据
    List<SdStudentAttenceDetail> getAllWeekendRecordsByClassIds(@Param("classIds") List<Long> classIds,
                                                                @Param("accessDate") String accessDate,
                                                                @Param("attenceStatus") String attenceStatus);
    
    // 获取月度考勤统计名单
    List<SdStudentAttenceDetail> getMonthlyAttenceStatisticsData(List<Long> classIds, String accessDate, String attenceStatus);
    
    List<SdStudentAttenceDetail> getMonthlyWeekendStatisticsData(List<Long> classIds, String accessDate, String attenceStatus);
}
